using System;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using VSSAHT.Transfer;
using VSS_AHT.DataAccess;
using System.Collections.Generic;
using System.Diagnostics;

namespace VSSAHT.DataAccess
{
	public class UserDataAccess : UserDataAccessBase, IUserDataAccess
	{


        private UserDTO _searchObject;
        /// <summary>
        /// Search Object Property
        /// </summary>
        public UserDTO SearchObject
        {
            get
            {
                return _searchObject;
            }
            set
            {
                if (_searchObject != value)
                {
                    _searchObject = value;
                }
            }
        }

        #region Methods

        public IList<UserDTO> Search()
        {
            IList<UserDTO> collection = null;
            try
            {
                using (VSSAHTDataContext dataContext = new VSSAHTDataContext(base.ConnectionString))
                {
                    IQueryable<User> query = dataContext.Users;
                    if (!String.IsNullOrEmpty(_searchObject.Username))
                    {
                        query = query.Where(e => e.Username.Contains(_searchObject.Username));
                        //hasConditions = true;
                    }
                   

                    collection = new List<UserDTO>();
                    foreach (var item in query)
                    {
                        collection.Add(UserMapper.ToBusinessObject(item));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return collection;
        }
        #endregion

        public bool Authentication(ref UserDTO user)
        {
            bool result = false;
            try
            {
                using (IDbConnection connection = DbProviderFactory.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;
                    using (VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
                    {
                        string username = user.Username;
                        string passwordEncrypt = user.PasswordEncrypt;
                        // Get last entity from database
                        var entity = biocode.Users.SingleOrDefault(u => u.Username == username && u.PasswordEncrypt == passwordEncrypt);

                        // If entity exists
                        if (entity != null)
                        {
                            // Set value to model
                            user = UserMapper.ToBusinessObject(entity);
                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Get last failed
                Debug.WriteLine(ex.Message);
            }

            return result;
        }
	}
}
